<template>
    <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
        <span class="el-form_span">学员管理系统</span>
        <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="验证码" prop="Verification" class="Verification-_input">
            <el-input v-model="ruleForm.Verification" @keyup.enter="submitForm(ruleFormRef)" />
            <span class="Verification-_input_span" @click="Refreshcode" v-html="reslist.img" v-loading="loading"
                element-loading-background="rgba(0, 0, 0,.3)"></span>
        </el-form-item>
        <el-form-item class="smsLogin">
            <el-button type="primary" @click="submitForm(ruleFormRef)" style="width: 200px;">登录</el-button>
        </el-form-item>

        <span class="form_span" @click="switchSmsLogin"><el-icon>
                <PhoneFilled />
            </el-icon>短信登录入口</span>
    </el-form>

</template>
<script setup>
import { reactive, ref, onMounted } from 'vue'
import emitter from "../../utils/bus/index.js"
import { rules, useSubmitForm } from "../../utils/validata/validata.js"
import { useRouter } from "vue-router"
import { ElLoading } from 'element-plus'
import { useUser } from "../../store/uses.js"

import * as api from "../../api/user.js"


let loadingInstance = ref()

// loadingInstance.value = ElLoading.service({
//     fullscreen: true,
//     text: "还在加载中",
//     background: "rgba(0,0,0,.3)"
// })
// setTimeout(() => {
//     loadingInstance.value.close()
// }, 3333)


let reslist = ref("")
let isopen = ref(true)
let ruleFormRef = ref()

const loading = ref(true)
let getuser = useUser()
const router = useRouter()

let switchSmsLogin = () => {
    console.log("yyyy");
    emitter.emit("isopen", isopen.value = !isopen.value)
}

const ruleForm = reactive({
    username: 'admin',
    password: 'admin',
    Verification: '',
})



//获取验证码方法
let Refreshcode = async () => {
    loading.value = true
    await api.getCode().then(res => {
        console.log(res, "iiii");
        if (res.data.state) {
            loading.value = false
            reslist.value = res.data
        }
    })
}

let successForm = () => {
    Verificationcode()
}
let failForm = () => {
    console.log("lllll");
}

const submitForm = useSubmitForm(successForm, failForm)

//校验验证码接口方法并登录
let Verificationcode = async () => {
    try {
        // let res = await api.getCaptchatt(ruleForm.Verification)

        // loadingInstance.value = ElLoading.service({
        //     fullscreen: true,
        //     text: "还在加载中",
        //     background: "rgba(0,0,0,.3)"
        // })

        let reslogin = await api.getLogin(ruleForm.username, ruleForm.password)
        console.log(reslogin.data, "sssssss");
        sessionStorage.setItem("permission", JSON.stringify(reslogin.data.permission.buttons))
        sessionStorage.setItem("token", reslogin.data.token)
        // console.log(reslogin.data.userInfo, "gdgdfgdfgdf");
        getuser.$patch((state) => {
            state.usersifno = reslogin.data.userInfo
        })
        router.push({
            path: '/'
        })

        loadingInstance.value.close()

    } catch (error) {
        console.log(error);
        Refreshcode()
    }

}

// let login = async (username, password) => {
//     const reslogin = await api.getLogin(username, password)
//     console.log(reslogin, "kkkk")
//     router.push({
//         name: 'home'
//     })
//     return reslogin
// }

onMounted(() => {
    Refreshcode()
})

</script>
  
<style lang="less" scoped>
.el-form-item__content {
    .el-input {
        width: 200px;
    }
}

.el-form {
    background-color: rgba(0, 0, 0, .3);
    width: 400px;
    height: 400px;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    position: absolute;
    bottom: 10%;
    right: 7%;

    .el-form-item__label {
        color: rgb(223, 210, 210);
    }

}

.from-el-conter {
    position: relative;
    height: 100vh;

}

.videoItem {
    position: fixed;
    width: 100vw;
    // z-index: -39857934;
    // height: 100vh;
}

.Verification-_input {
    :deep(.el-input__wrapper) {
        border-top-right-radius: 0px;
        border-bottom-right-radius: 0px;

    }

    :deep(.el-input__wrapper) {
        box-shadow: none;
    }

    .el-input {
        width: 100px;

    }
}

.el-form-item__content {
    display: flex;
    flex: 1;


    .Verification-_input_span {
        background-color: rgb(248, 245, 245);
        width: 100px;
        border-top-right-radius: 3px;
        border-bottom-right-radius: 3px;
        border-right: none;

        display: flex;
        align-items: center;
        justify-content: center;

        :deep(svg) {
            width: 100px;
            height: 32px;
        }
    }
}

.el-form_span {
    text-align: center;
    margin: 0 0 60px 0;
    font-size: 32px;
    color: #fff;
}

.el-form-item {
    :deep(.el-form-item__label) {
        color: rgb(235, 232, 232);
    }
}

.form_span {
    color: rgb(234, 218, 218);
    text-align: center;
    cursor: pointer;

    &:hover {
        color: red;
    }
}
</style>
    